#include "Camera.h"

Camera::Camera()
    : m_position(0.0f, 10.0f, -20.0f)
    , m_target(0.0f, 0.0f, 0.0f)
    , m_up(0.0f, 1.0f, 0.0f)
    , m_fov(D3DX_PI / 4.0f)
    , m_aspect(16.0f / 9.0f)
    , m_nearPlane(1.0f)
    , m_farPlane(2000.0f)
    , m_followMode(false)
    , m_smoothPosition(0.0f, 10.0f, -20.0f)
{
    D3DXMatrixIdentity(&m_matView);
    D3DXMatrixIdentity(&m_matProj);
}

Camera::~Camera()
{
}

void Camera::SetPosition(const D3DXVECTOR3& pos)
{
    m_position = pos;
}

void Camera::SetTarget(const D3DXVECTOR3& target)
{
    m_target = target;
}

void Camera::SetUpVector(const D3DXVECTOR3& up)
{
    m_up = up;
}

void Camera::SetPerspective(float fov, float aspect, float nearPlane, float farPlane)
{
    m_fov = fov;
    m_aspect = aspect;
    m_nearPlane = nearPlane;
    m_farPlane = farPlane;
}

void Camera::UpdateMatrices(LPDIRECT3DDEVICE9 device)
{
    // 更新视图矩阵
    D3DXMatrixLookAtLH(&m_matView, &m_position, &m_target, &m_up);
    device->SetTransform(D3DTS_VIEW, &m_matView);

    // 更新投影矩阵
    D3DXMatrixPerspectiveFovLH(&m_matProj, m_fov, m_aspect, m_nearPlane, m_farPlane);
    device->SetTransform(D3DTS_PROJECTION, &m_matProj);
}

void Camera::ProcessInput(float deltaTime)
{
    float moveSpeed = 200.0f * deltaTime;  // 增加移动速度以便更明显

    // 计算前向和右向量
    D3DXVECTOR3 forward = m_target - m_position;
    forward.y = 0.0f;  // 保持水平移动
    D3DXVec3Normalize(&forward, &forward);

    D3DXVECTOR3 right;
    D3DXVec3Cross(&right, &forward, &m_up);  // 修正叉积顺序
    D3DXVec3Normalize(&right, &right);

    D3DXVECTOR3 movement(0.0f, 0.0f, 0.0f);
    bool moved = false;

    // 检测WASD按键 (使用高位测试)
    if (GetAsyncKeyState('W') & 0x8000)
    {
        movement += forward * moveSpeed;
        moved = true;
    }
    if (GetAsyncKeyState('S') & 0x8000)
    {
        movement -= forward * moveSpeed;
        moved = true;
    }
    if (GetAsyncKeyState('A') & 0x8000)
    {
        movement -= right * moveSpeed;  // 左移
        moved = true;
    }
    if (GetAsyncKeyState('D') & 0x8000)
    {
        movement += right * moveSpeed;  // 右移
        moved = true;
    }

    // 应用移动
    if (moved)
    {
        m_position += movement;
        m_target += movement;
    }
}

void Camera::Move(const D3DXVECTOR3& offset)
{
    m_position += offset;
    m_target += offset;
}

void Camera::FollowTarget(const D3DXVECTOR3& targetPos, float targetYaw, float turretYaw, float deltaTime)
{
    // Camera follows behind and above the tank
    float totalYaw = targetYaw + turretYaw;  // Follow turret direction

    // Calculate desired camera position (behind and above the gun)
    float distance = 30.0f;  // Distance behind tank
    float height = 15.0f;    // Height above tank

    D3DXVECTOR3 desiredPos;
    desiredPos.x = targetPos.x - sinf(totalYaw) * distance;
    desiredPos.y = targetPos.y + height;
    desiredPos.z = targetPos.z - cosf(totalYaw) * distance;

    // Smooth camera movement (lerp)
    float smoothSpeed = 5.0f * deltaTime;
    if (smoothSpeed > 1.0f) smoothSpeed = 1.0f;

    m_smoothPosition = m_smoothPosition * (1.0f - smoothSpeed) + desiredPos * smoothSpeed;
    m_position = m_smoothPosition;

    // Look at a point slightly ahead of the tank
    D3DXVECTOR3 lookAhead;
    lookAhead.x = targetPos.x + sinf(totalYaw) * 10.0f;
    lookAhead.y = targetPos.y + 5.0f;
    lookAhead.z = targetPos.z + cosf(totalYaw) * 10.0f;

    m_target = lookAhead;
}
